package com.huige.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author 朱志辉
 * @Create 2024/7/20 下午5:52
 * @Version 1.0
 * Description: 拦截器
 */
public class MyInterceptor implements HandlerInterceptor{


    //在执行handler之前！调用的拦截器方法！
    //编码格式设置，登录保护。权限处理！！

    /**
     *  filter - doFilter - doFilterInternal
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 调用的方法对象
     * @return true：放行，false：拦截
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
    }

    /**
     *  调用完handler之后！调用的拦截器方法！没有拦截机制了
     *
     * 对结果处理！敏感词汇检查！！
     *
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 调用的方法对象
     * @param modelAndView 调用完handler之后，返回的视图对象
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }


    /**
     * 整体处理完毕！！
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 调用的方法对象
     * @param ex    handler报错了 异常对象
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}
